# -*- makefile -*-

# this might be useful for some future work if we want to make
# actions more variable.
define .Make-rule
$1:
	$2

endef

############################################################
# useful functions for the install methods mentioned below
############################################################
# $(call .FUN-install-copy,file1 file2 file3,dir1/ dir2/)
# copies whichever file exists into each of ${INSTALL/}dir
# mkdir-ing as necessary.
define .FUN-install-copy
	@ files='$$(strip $1)'; dirs='$$(strip $2)'; \
	if [ -n "$$$${files}" -a -n "$$$${dirs}" ] ; then \
	  for F in $$$${files} ; do \
	    if [ -f $$$${F} ] ; then \
	      for D in $$$${dirs} ; do \
	        Fout=$${INSTALL/}$$$${D}`basename $$$${F}` ; \
	        mkdir -p `dirname $$$${Fout}` && \
	        rm -f $$$${Fout} && cp -fp $$$${F} $$$${Fout} ; \
	      done ; \
	    fi ; \
	  done ; \
        fi
endef
# because SOME PLATFORMS (like cygwin) use a special .exe extension
# in executables, we have to do a little hack here.  We assume that
# Make.compilers has set a variable called .EXE
define .FUN-install-copy-exe
	@ files='$$(strip $1)'; dirs='$$(strip $2)'; \
	if [ -n "$$$${files}" -a -n "$$$${dirs}" ] ; then \
	  for F in $$$${files} ; do \
	    if [ "${.EXE}" != "" -a -f $$$${F}${.EXE} ] ; then \
	      for D in $$$${dirs} ; do \
	        Fout=$${INSTALL/}$$$${D}`basename $$$${F}` ; \
	        mkdir -p `dirname $$$${Fout}` && \
	        rm -f $$$${Fout}${.EXE} && cp -fp $$$${F}${.EXE} $$$${Fout}${.EXE} ; \
	      done ; \
	    fi ; \
	    if [ -f $$$${F} ] ; then \
	      for D in $$$${dirs} ; do \
	        Fout=$${INSTALL/}$$$${D}`basename $$$${F}` ; \
	        mkdir -p `dirname $$$${Fout}` && \
	        rm -f $$$${Fout} && cp -fp $$$${F} $$$${Fout} ; \
	      done ; \
	    fi ; \
	  done ; \
        fi
endef
# we do another cygwin inspired hack to deal with that fact that
# .so shlibs need to be turned into .dll files.
define .FUN-install-copy-shlib
	@ files='$$(strip $1)'; dirs='$$(strip $2)'; \
	if [ -n "$$$${files}" -a -n "$$$${dirs}" ] ; then \
	  for F in $$$${files} ; do \
	    if [ -f $$$${F} ] ; then \
	      for D in $$$${dirs} ; do \
	        Fout=$${INSTALL/}$$$${D}`basename $$$${F} .so`${.SO} ; \
	        mkdir -p `dirname $$$${Fout}` && \
	        rm -f $$$${Fout} && cp -fp $$$${F} $$$${Fout} ; \
	      done ; \
	    fi ; \
	  done ; \
        fi
endef
# use this one for executable scripts with #!  substitution
#	        echo ":Mangling $$$${F} to $$$${Fout}:" ;
define .FUN-install-copy-script
	@ files='$$(strip $1)'; dirs='$$(strip $2)'; sheb='$$(strip $3)'; \
	if [ -n "$$$${files}" -a -n "$$$${dirs}" ] ; then \
	  for F in $$$${files} ; do \
	    if [ -f $$$${F} ] ; then \
	      for D in $$$${dirs} ; do \
	        Fout=$${INSTALL/}$$$${D}`basename $$$${F}` ; \
	        mkdir -p `dirname $$$${Fout}` && \
	        rm -f $$$${Fout} && cp -fp $$$${F} $$$${Fout} ; \
	        chmod ugo+x $$$${Fout} && \
	        ${PERL} -npi \
	           -e"if(0==\$$$$i++){s|^#!.*|#!$$$${sheb}|}" $$$${Fout}; \
	      done ; \
	    fi ; \
	  done ; \
        fi
endef


############################################################
# C and C++ stuff
############################################################
# Building depends goals for C/CXX things

# C_SRCS and CXX_SRCS are collected together and turned into
# associated *.d dependency files.

# WISHLIST: does not propagate failure to the parent make
# for some reason.  It really should.  I think the pipe to
# sed masks the exit code.
__DEPGOALS__     +=                 $$(patsubst %,%.d,$${${1:.=.C_SRCS}})
ALL_C_DEPS       :=$(foreach x,${//},$(patsubst %,%.d,${${x:.=.C_SRCS}}))
${ALL_C_DEPS}:%.d:%
	@ echo "making $@"
	@ dir=`echo $< | sed -e's~[^/]*$$~~'`; \
	  ${CCDEP} ${CDEPFLAGS} ${CFLAGS} $< | \
	  sed -e"/:/s!^!$${dir}!" > $@

__DEPGOALS__     +=                 $$(patsubst %,%.d,$${${1:.=.CXX_SRCS}})
ALL_CXX_DEPS     :=$(foreach x,${//},$(patsubst %,%.d,${${x:.=.CXX_SRCS}}))
${ALL_CXX_DEPS}:%.d:%
	@ echo "making $@"
	@ dir=`echo $< | sed -e's~[^/]*$$~~'`; \
	  ${CXXDEP} ${CXXDEPFLAGS} ${CXXFLAGS} $< | \
	  sed -e"/:/s!^!$${dir}!" > $@


######  generic pattern rules for subgoals
# don't want .o's getting deleted as intermediates
.PRECIOUS: %${.O}
.SUFFIXES: ${.O}

%${.O}: %.c
	${-CC} ${CC} ${CFLAGS} ${CFLAGS_COMPILE} -o $@ -c $<

%${.O}: %.cc
	${-CXX} ${CXX} ${CXXFLAGS} ${CXXFLAGS_COMPILE} -o $@ -c $<

%${.O}: %.cpp
	${-CXX} ${CXX} ${CXXFLAGS} ${CXXFLAGS_COMPILE} -o $@ -c $<

%${.O}: %.C
	${-CXX} ${CXX} ${CXXFLAGS} ${CXXFLAGS_COMPILE} -o $@ -c $<


# linking commands use the $+ to get duplicated prereqs for linking
## EXE targets
ALL_C_EXES   :=$(strip $(foreach x,${//},${${x:.=.C_EXES}}))
${ALL_C_EXES}:
	${-CC} ${CLD} ${CLDFLAGS} -o $@ $+ ${CLIBS}
__SUBGOALS__+=$${${1:.=.C_EXES}}

ALL_CXX_EXES :=$(strip $(foreach x,${//},${${x:.=.CXX_EXES}}))
${ALL_CXX_EXES}:
	${-CXX} ${CXXLD} ${CXXLDFLAGS} -o $@ $+ ${CXXLIBS}
__SUBGOALS__+=$${${1:.=.CXX_EXES}}

define .RULE-install-copy-C-CXX-EXES
${1:.=.install-copy}: ${1:.=.install-copy-C-CXX-EXES}
${1:.=.install-copy-C-CXX-EXES}:
	$(call .FUN-install-copy-exe,$${${1:.=.C_EXES}} $${${1:.=.CXX_EXES}},bin/)

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-C-CXX-EXES,$x)))


## LIB targets
ALL_C_LIBS   :=$(strip $(foreach x,${//},${${x:.=.C_LIBS}}))
${ALL_C_LIBS}:
	${-CC} ${RM} $@ && ${AR} ${ARFLAGS} $@ $^
__SUBGOALS__+=$${${1:.=.C_LIBS}}

ALL_CXX_LIBS     :=$(strip $(foreach x,${//},${${x:.=.CXX_LIBS}}))
${ALL_CXX_LIBS}:
	${-CXX} ${RM} $@ && ${AR} ${ARFLAGS} $@ $^
__SUBGOALS__+=$${${1:.=.CXX_LIBS}}

## Shared targets
# AIX has really weird shared lib building flags.  Unfortunately, I could
# not think of a way out of this hack.
${_OS_}_SHLIB_FLAGS:=
ALL_C_SHLIBS     :=$(strip $(foreach x,${//},${${x:.=.C_SHLIBS}}))
${ALL_C_SHLIBS}:
	${-CC} ${RM} $@ && ${CC} ${CLDFLAGS} ${SHLIB_FLAGS} ${${_OS_}_SHLIB_FLAGS} -o $@ $^ ${CLIBS}

ALL_CXX_SHLIBS   :=$(strip $(foreach x,${//},${${x:.=.CXX_SHLIBS}}))
${ALL_CXX_SHLIBS}:
	${-CXX} ${RM} $@ && ${CXX} ${CXXLDFLAGS} ${SHLIB_FLAGS} ${${_OS_}_SHLIB_FLAGS} -o $@ $^ ${CXXLIBS}
__SUBGOALS__+=$${${1:.=.C_SHLIBS}} $${${1:.=.CXX_SHLIBS}}

define .RULE-install-copy-C-CXX-LIBS
${1:.=.install-copy}: ${1:.=.install-copy-C-CXX-LIBS}
${1:.=.install-copy-C-CXX-LIBS}:
	$(call .FUN-install-copy,$${${1:.=.C_LIBS}} $${${1:.=.CXX_LIBS}}, lib/$${${1.=.LIB/}})

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-C-CXX-LIBS,$x)))

define .RULE-install-copy-C-CXX-SHLIBS
${1:.=.install-copy}: ${1:.=.install-copy-C-CXX-SHLIBS}
${1:.=.install-copy-C-CXX-SHLIBS}:
	$(call .FUN-install-copy-shlib, $${${1:.=.C_SHLIBS}} $${${1:.=.CXX_SHLIBS}}, lib/$${${1.=.LIB/}})

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-C-CXX-SHLIBS,$x)))



define .RULE-install-copy-C-CXX-INCS
${1:.=.install-copy}: ${1:.=.install-copy-C-CXX-INCS}
${1:.=.install-copy-C-CXX-INCS}:
	$(call .FUN-install-copy,$${${1:.=.C_INCS}} $${${1:.=.CXX_INCS}}, include/$${${1:.=.INCLUDE/}})

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-C-CXX-INCS,$x)))



############################################################
# latex and some ps/pdf stuff
############################################################

# I'm not sure if I should mark these as precious or not
#.PRECIOUS: %.pdf %.ps %.dvi %.aux %.bbl

%.dvi: %.tex
	${-LATEX} cd `dirname $<` && ${LATEX} `basename $<` && ${LATEX} `basename $<`

%.aux: %.tex
	${-LATEX} cd `dirname $<` && ${LATEX} `basename $<` && ${LATEX} `basename $<`

%.bbl: %.aux
	${-LATEX} cd `dirname $<` && ${BIBTEX} `basename ${<:.aux=}`


ALL_TEX_PS    :=$(strip $(foreach x,${//},${${x:.=.TEX_PS}}))
ALL_TEX_PDF   :=$(strip $(foreach x,${//},${${x:.=.TEX_PDF}}))

${ALL_TEX_PS}: %.ps: %.dvi
	${-LATEX} cd `dirname $<` && ${DVIPS} -o `basename $@` `basename $<`

${ALL_TEX_PDF}: %.pdf: %.tex %.aux 
	${-LATEX} cd `dirname $<` && ${PDFLATEX} `basename $<` && ${PDFLATEX} `basename $<` 
__SUBGOALS__+=$${${1:.=.TEX_PS}} $${${1:.=.TEX_PDF}}

   # install rules
define .RULE-install-copy-TEX_PSPDF
${1:.=.install-copy}: ${1:.=.install-copy-TEX_PSPDF}
${1:.=.install-copy-TEX_PSPDF}:
# TEX_PS go to doc/
	$(call .FUN-install-copy,$${${1:.=.TEX_PS}},doc/$${${1:.=.DOC/}})
# TEX_PDF go to doc/
	$(call .FUN-install-copy,$${${1:.=.TEX_PDF}},doc/$${${1:.=.DOC/}})

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-TEX_PSPDF,$x)))



############################################################
# Python
############################################################

# python exes and libs are not subgoals.
# Otherwise, they'd be deleted when we cleaned.
# if we ever introduce some notion of 'file prep', beyond
# adding the she-bang line, then we might want to do something
# different here.
#__SUBGOALS__+=$${${1:.=.PY_EXES}} $${${1:.=.PY_LIBS}}

define .RULE-install-copy-PYTHON
${1:.=.install-copy}: ${1:.=.install-copy-PYTHON}
${1:.=.install-copy-PYTHON}:
	$(call .FUN-install-copy-script, $${${1:.=.PY_EXES}}, bin/, ${PYTHON} ${PYTHON_FLAGS})
	$(call .FUN-install-copy,        $${${1:.=.PY_LIBS}}, lib/$${${1:.=.PY_LIB/}})

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-PYTHON,$x)))


############################################################
# Perl
############################################################
# Same sort of spiel as python
#__SUBGOALS__+=$${${1:.=.PERL_EXES}} $${${1:.=.PERL_LIBS}}

define .RULE-install-copy-PERL
${1:.=.install-copy}: ${1:.=.install-copy-PERL}
${1:.=.install-copy-PERL}:
	$(call .FUN-install-copy-script, $${${1:.=.PERL_EXES}}, bin/, ${PERL} ${PERL_FLAGS})
	$(call .FUN-install-copy,        $${${1:.=.PERL_LIBS}}, lib/$${${1:.=.PERL_LIB/}})

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-PERL,$x)))

############################################################
# sh
############################################################
# Same sort of spiel as python
#__SUBGOALS__+=$${${1:.=.SH_EXES}} $${${1:.=.SH_LIBS}}

define .RULE-install-copy-SH
${1:.=.install-copy}: ${1:.=.install-copy-SH}
${1:.=.install-copy-SH}:
	$(call .FUN-install-copy-script, $${${1:.=.SH_EXES}}, bin/, ${SH} ${SH_FLAGS})
	$(call .FUN-install-copy,        $${${1:.=.SH_LIBS}}, lib/$${${1:.=.SH_LIB/}})

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-SH,$x)))


############################################################
# share -- a random catchall for scripts and whatnot that
# we should have real rules for but we don't right now
############################################################
#__SUBGOALS__+=$${${1:.=.SHARES}}

define .RULE-install-copy-SHARE
${1:.=.install-copy}: ${1:.=.install-copy-SHARE}
${1:.=.install-copy-SHARE}:
	$(call .FUN-install-copy,$${${1:.=.SHARES}}, share/$${${1:.=.SHARE/}})

endef
$(eval $(foreach x,${//},$(call .RULE-install-copy-SHARE,$x)))
